<table cellpadding="0" cellspacing="0">
	<tbody>
        <tr>
            <td class="leftside">
           		<?php require_once("menu.php");?>
            </td>
            <td class="rightside">
            <?php
            	$get_cat_name = 'SELECT
									  `category`.`cat_name`, `subcat`.*
									FROM
									  `subcat` INNER JOIN
									  `category` ON `category`.`cat_id` = `subcat`.`cat_id`
									WHERE
									  `subcat`.`cat_id` = '.$_GET['cid'];	
				$cat_q = mysql_query($get_cat_name);
				$cat_name = mysql_fetch_assoc($cat_q);
			?>
                <div class="box" style="margin-left:10px; margin-right: 10px; float:left;">
                	<div id='title_menu' class="ui-corner-top">เพิ่มหมวดหมู่ย่อยของ <?php echo $cat_name['cat_name'];?></div>  
                    <div class="content" style="min-height: 150px;">
                   	<?php if($_SESSION['level'] == 'superadministrator'){?>
                    <?php 
						$get_last_cat_record = mysql_query('SELECT MAX(sub_id) AS last_sub FROM subcat WHERE cat_id != 99');
						$last_cat_id = mysql_fetch_assoc($get_last_cat_record);
					?>
                	  <table width="462" border="0" cellspacing="10">
                        <tbody>
                       		<tr>
                            	<td width="126" align="right"><strong>ชื่อหมวดหมู่ย่อย:</strong></td>
                                <td colspan="4" align="left"><input type="text" id="sub_name" /></td>
                            </tr> 
                            <tr>
                            	<td align="right"><strong>สถานะ:</strong></td>
                                <td align="left" width="20"><input type="radio" name="status" value="1" /></td>
                                <td align="left" width="49"><strong>ทำงาน</strong></td>
                                <td align="left" width="20"><input type="radio" name="status" value="0" checked="checked"/></td>
                                <td width="177" align="left"><strong>ไม่ทำงาน</strong></td>
                            </tr> 
                            <tr>
                            	<td></td>
                                <td colspan="4" align="left">
                                	<input type="hidden" id="cat_id" value="<?php echo $_GET['cid']; ?>" />
                                	<input type="button" id="subsubmit" value="เพิ่มหมวดหมู่ย่อย" /><span id="loading"></span>
                                </td>
                            </tr> 
                        </tbody>
                      </table>
				  <?php }?>
                  </div>
                </div>
                
                <div class="box" style="margin-left:10px; margin-right: 10px; float:left;">
                	<div id='title_menu' class="ui-corner-top">หมวดย่อย</div>  
                    <div class="content" style="min-height: 150px;">
                	  <table class="list">
                        <thead>
                          <tr align="center">
                            <td>ลำดับ</td>
                            <td>หมวดหมู่หลัก</td>
                            <td>ชื่อหมวดหมู่ย่อย</td>
                            <td>สถานะ</td>
                            <td>ลบ</td>                         
                          </tr>
                        </thead>
                        <tbody>
                        <?php
						if(isset($_GET['status'])){
							mysql_query('UPDATE subcat SET sub_active='.$_GET['status'].' WHERE sub_id='.$_GET['id']);
						}
					
						$sql = 'SELECT * FROM subcat WHERE cat_id = '.$_GET['cid'];
						$q = $db->select_query($sql);
						$c = 1;
                        while($rs = $db->fetch($q)){							
						?>
                          <tr align="center">
                          	<td><?php echo $c++;?></td>
                            <td><strong><?php echo $cat_name['cat_name'];?></strong></td>
                            <td><strong class="dblclick" id="<?php echo $rs['sub_id'];?>"><?php echo $rs['sub_name'];?></strong></td>
                            <td>
                            <?php ($rs['sub_active']==1)? 
									print '<a href="?m=posts&p=subcat&cid='.$_GET['cid'].'&id='.$rs['sub_id'].'&status=0"><strong style="color:green;">ทำงาน</strong></a>' : 
									print '<a href="?m=posts&p=subcat&cid='.$_GET['cid'].'&id='.$rs['sub_id'].'&status=1"><strong style="color:red;">ไม่ทำงาน</strong></a>';?>
                            </td>
                            <td>
                            <?php
                            if(isset($_SESSION['level']) && ($_SESSION['level'] == 'superadministrator')){
							?>
                            <a href="#" id="delsubcat" sid="<?php echo $rs['sub_id'];?>"><img src="images/delete.png" title="ลบหมวดหมู่นี้"/></a>
                            <?php }else{ ?>
                 			<a href="#" onclick="alert('คุณไม่มีสิทธิ์ในการลบหมวดหมู่ใดๆ ทั้งนั้น!!!');return false;" ><img src="images/delete.png" title="ลบหมวดหมู่นี้"/></a>           
                            <?php } ?>
                            </td>
                          </tr>  
                          
                          <?php
							
						}
						  ?>
                        </tbody>
                   	</table>
                  	</div>
                </div>            
                
            </td>
        </tr>
   	</tbody>
</table>
<script type="text/javascript">
	$(".dblclick").editable("admin_query.php?editsubcat=true", { 
	  id   : 'dblclick',
	  indicator	: 'กำลังบันทึก...',
      tooltip   : "Doubleclick to edit...",
      event     : "dblclick",
      style  : "inherit"
  	});
</script>